package com.liuyu.lottery.controller;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.Set;
import java.util.TreeSet;


@Slf4j
@RestController
@RequestMapping("/lottery")
public class LotteryController {

    /**
     * 生成code
     *
     * @return
     */
    @GetMapping("/gen")
    public String genLottery() {
        //判断当前日期
        StringBuilder sb = genCode();
        return sb.toString();
    }

    @GetMapping("/genByCode")
    public String getLotteryByCode(@RequestParam(required = true) String code) {
        String result = genLotteryByCode(code);
        return result;

    }

    @GetMapping("/genDaLeTou")
    public String getDaleTouLotteryByCode(@RequestParam(required = true) String code) {
        String result = genDaLeLotteryByCode(code);
        return result;
    }

    private String genDaLeLotteryByCode(String code) {
        if (!StrUtil.contains(code, ",")) {
            return "code not contains ,";
        }
        int num;
        String[] codeArr = code.split(",");
        Set<Integer> redBaallSet = new TreeSet<>();
        Set<Integer> blueBaallSet = new TreeSet<>();
        for (int i = 0; i < codeArr.length - 2; i++) {
            num = Integer.parseInt(codeArr[i]) + RandomUtil.randomInt(-35, 35, true, true);
            while (num > 35 || num < 1 || redBaallSet.contains(num)) {
                num = Integer.parseInt(codeArr[i]) + RandomUtil.randomInt(-35, 35, true, true);
            }
            redBaallSet.add(num);
        }
        for (int i = codeArr.length - 2; i < codeArr.length; i++) {
            num = Integer.parseInt(codeArr[i]) + RandomUtil.randomInt(-12, 12, true, true);
            while (num > 12 || num < 1 || blueBaallSet.contains(num)) {
                num = Integer.parseInt(codeArr[i]) + RandomUtil.randomInt(-12, 12, true, true);
            }
            blueBaallSet.add(num);
        }
        StringBuilder sb = new StringBuilder();
        sb.append("红球:").append(StrUtil.join(",", redBaallSet));
        sb.append("\n");
        sb.append("篮球:").append(StrUtil.join(",", blueBaallSet));

        return sb.toString();
//        return null;
    }

    /**
     * 根据输入的code生成一个随机的新的code
     *
     * @param code 输入开奖序列，以,分割 实例 01,02,03,04,05,06,10
     * @return 生成结果
     */
    private String genLotteryByCode(String code) {
        if (!StrUtil.contains(code, ",")) {
            return "code not contains ,";
        }
        int num;
        String[] codeArr = code.split(",");
        Set<Integer> redBaallSet = new TreeSet<>();
        Set<Integer> blueBaallSet = new TreeSet<>();
        for (int i = 0; i < codeArr.length - 1; i++) {
            num = Integer.parseInt(codeArr[i]) + RandomUtil.randomInt(-33, 33, true, true);
            while (num > 33 || num < 1 || redBaallSet.contains(num)) {
                num = Integer.parseInt(codeArr[i]) + RandomUtil.randomInt(-33, 33, true, true);
            }
            redBaallSet.add(num);
        }
        for (int i = codeArr.length - 1; i < codeArr.length; i++) {
            num = Integer.parseInt(codeArr[i]) + RandomUtil.randomInt(-16, 16, true, true);
            while (num > 16 || num < 1 || blueBaallSet.contains(num)) {
                num = Integer.parseInt(codeArr[i]) + RandomUtil.randomInt(-16, 16, true, true);
            }
            blueBaallSet.add(num);
        }
        StringBuilder sb = new StringBuilder();
        sb.append("红球:").append(StrUtil.join(",", redBaallSet));
        sb.append("\n");
        sb.append("篮球:").append(StrUtil.join(",", blueBaallSet));

        return sb.toString();
    }



    private static StringBuilder genCode() {
        StringBuilder sb = new StringBuilder();
        sb.append("红球:");
        Set<Integer> redBallSet = new TreeSet<>();
        Random random = new Random();
        while (redBallSet.size() < 6) {
            int i = random.nextInt(33) + 1;
            redBallSet.add(i);
        }
        sb.append(StrUtil.join(",", redBallSet));
        log.info("set:{}", redBallSet);
        sb.append("\n");
        sb.append("篮球:");
        sb.append(random.nextInt(16) + 1);
        return sb;
    }
}
